import { getMongoRepository, ObjectID } from 'typeorm';

import { Friends } from '../models/Friend';

class FriendService {
  // 根据 uid(当前登录的用户id) 查找好友列表
  findByUid(uid: string) {
    const friendRepository = getMongoRepository(Friends)
    return friendRepository.find({
      where: {
        uid
      }
    })
  }

  // 创建好友
  save(friend: Friends) {
    const friendRepository = getMongoRepository(Friends)
    return friendRepository.save(friend)
  }

  // 根据 uid 和 fid 查找好友
  findByUidAndFid(uid: string, fid: string) {
    const friendRepository = getMongoRepository(Friends)
    return friendRepository.findOne({
      where: {
        uid,
        fid
      }
    })
  }

  // 移除好友
  remove(uid: String, fid: String) {
    const friendRepository = getMongoRepository(Friends)
    return friendRepository.deleteOne({uid, fid})
  }

}

export default new FriendService()